import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";
import _forEach from "lodash/forEach";

var _dec, _dec2, _dec3, _dec4, _dec5, _dec6, _dec7, _dec8, _dec9, _dec10, _dec11, _dec12, _class, _class2;

/**
 * @date 2018/11/20
 */
import React from 'react';
import { Bind } from 'lodash-decorators';
import { connect } from 'dva';
import { routerRedux } from 'dva/router';
import { Button as ButtonPermission } from 'components/Permission';
import formatterCollections from 'utils/intl/formatterCollections';
import { getCurrentOrganizationId } from 'utils/utils';
import intl from 'utils/intl';
import { Header, Content } from 'components/Page';
import EditModal from "../../components/EditModal";
import SearchForm from "./SearchForm";
import DataTable from "./DataTable";
var PageList = (_dec = connect(function (_ref) {
  var loading = _ref.loading,
      uiPageOrg = _ref.uiPageOrg;
  return {
    fetchPageListLoading: loading.effects['uiPageOrg/fetchPageList'],
    createLoading: loading.effects['uiPageOrg/listCreateOne'],
    updateLoading: loading.effects['uiPageOrg/listUpdateOne'],
    uiPage: uiPageOrg,
    organizationId: getCurrentOrganizationId()
  };
}), _dec2 = formatterCollections({
  code: ['cpfm.ui']
}), _dec3 = Bind(), _dec4 = Bind(), _dec5 = Bind(), _dec6 = Bind(), _dec7 = Bind(), _dec8 = Bind(), _dec9 = Bind(), _dec10 = Bind(), _dec11 = Bind(), _dec12 = Bind(), _dec(_class = _dec2(_class = (_class2 = /*#__PURE__*/function (_React$Component) {
  _inherits(PageList, _React$Component);

  var _super = _createSuper(PageList);

  function PageList() {
    var _this;

    _classCallCheck(this, PageList);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));
    _this.state = {
      pagination: {},
      // Table 切换分页后的分页信息
      // Modal
      createModalProps: {
        visible: false
      },
      updateModalProps: {
        visible: false
      }
    };
    return _this;
  }

  _createClass(PageList, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var pagination = this.state.pagination;
      this.handleSearch(pagination);
    }
  }, {
    key: "reloadList",
    value: function reloadList() {
      var pagination = this.state.pagination;
      this.handleSearch(pagination);
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props = this.props,
          _this$props$uiPage = _this$props.uiPage,
          pagination = _this$props$uiPage.pagination,
          list = _this$props$uiPage.list,
          match = _this$props.match,
          fetchPageListLoading = _this$props.fetchPageListLoading,
          createLoading = _this$props.createLoading,
          updateLoading = _this$props.updateLoading;
      var _this$state = this.state,
          createModalProps = _this$state.createModalProps,
          updateModalProps = _this$state.updateModalProps,
          editRecord = _this$state.editRecord;
      return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Header, {
        title: intl.get('cpfm.ui.view.list.title').d('页面汇总')
      }, /*#__PURE__*/React.createElement(ButtonPermission, {
        icon: "plus",
        type: "primary",
        permissionList: [{
          code: "".concat(match.path, ".button.create"),
          type: 'button',
          meaning: '页面自定义(租户)-新建'
        }],
        onClick: this.handleCreateBtnClick
      }, intl.get('cpaas.common.button.create').d('新建'))), /*#__PURE__*/React.createElement(Content, null, /*#__PURE__*/React.createElement(SearchForm, {
        onSearch: this.handleSearch,
        onRefForm: this.handleRefSearchForm
      }), /*#__PURE__*/React.createElement(DataTable, {
        onPageEdit: this.handlePageEdit,
        onPageDesign: this.handlePageDesign,
        onTableChange: this.handleTableChange,
        dataSource: list.content,
        match: match,
        pagination: pagination,
        loading: fetchPageListLoading
      }), /*#__PURE__*/React.createElement(EditModal, {
        modalProps: createModalProps,
        confirmLoading: createLoading,
        onOk: this.handlePageCreate,
        onCancel: this.handlePageCreateCancel,
        title: intl.get('cpfm.ui.view.list.modal.create.title').d('新建页面'),
        isCreate: true
      }), /*#__PURE__*/React.createElement(EditModal, {
        modalProps: updateModalProps,
        confirmLoading: updateLoading,
        onOk: this.handlePageUpdate,
        onCancel: this.handlePageUpdateCancel,
        editRecord: editRecord,
        title: intl.get('cpfm.ui.view.list.modal.edit.title').d('编辑页面'),
        wrapClassName: "ant-modal-sidebar-right",
        transitionName: "move-right"
      })));
    }
  }, {
    key: "handleCreateBtnClick",
    value: function handleCreateBtnClick() {
      this.setState({
        createModalProps: {
          visible: true
        }
      });
    }
  }, {
    key: "handlePageCreate",
    value: function handlePageCreate(data) {
      var _this2 = this;

      var _this$props2 = this.props,
          dispatch = _this$props2.dispatch,
          organizationId = _this$props2.organizationId;
      dispatch({
        type: 'uiPageOrg/listCreateOne',
        payload: {
          organizationId: organizationId,
          data: data
        }
      }).then(function (res) {
        if (res) {
          _this2.setState({
            createModalProps: {
              visible: false
            }
          });

          _this2.reloadList();
        }
      });
    }
  }, {
    key: "handlePageCreateCancel",
    value: function handlePageCreateCancel() {
      this.setState({
        createModalProps: {
          visible: false
        }
      });
    }
    /**
     * 由于@Form.create 创造的高阶组件,所以必须使用自定义方法传递form
     * @param {Object} searchForm - 表单 form
     */

  }, {
    key: "handleRefSearchForm",
    value: function handleRefSearchForm(searchForm) {
      this.searchForm = searchForm;
    }
  }, {
    key: "handleSearch",
    value: function handleSearch() {
      var pagination = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      var params = this.searchForm.getFieldsValue();
      var filterUndefinedParams = {};

      _forEach(params, function (value, key) {
        if (value !== undefined) {
          filterUndefinedParams[key] = value;
        }
      });

      var _this$props3 = this.props,
          dispatch = _this$props3.dispatch,
          organizationId = _this$props3.organizationId;
      this.setState({
        pagination: pagination
      });
      dispatch({
        type: 'uiPageOrg/fetchPageList',
        payload: {
          organizationId: organizationId,
          params: _objectSpread({
            body: filterUndefinedParams
          }, pagination)
        }
      });
    }
  }, {
    key: "handleTableChange",
    value: function handleTableChange(_ref2) {
      var pagination = _ref2.pagination,
          sorter = _ref2.sorter;
      this.handleSearch({
        page: pagination,
        sort: sorter
      });
    }
  }, {
    key: "handlePageEdit",
    value: function handlePageEdit(record) {
      this.setState({
        editRecord: record,
        updateModalProps: {
          visible: true
        }
      });
    }
  }, {
    key: "handlePageUpdate",
    value: function handlePageUpdate(data) {
      var _this3 = this;

      var _this$props4 = this.props,
          dispatch = _this$props4.dispatch,
          organizationId = _this$props4.organizationId;
      dispatch({
        type: 'uiPageOrg/listUpdateOne',
        payload: {
          organizationId: organizationId,
          data: data
        }
      }).then(function (res) {
        if (res) {
          _this3.setState({
            updateModalProps: {
              visible: false
            }
          });

          _this3.reloadList();
        }
      });
    }
  }, {
    key: "handlePageUpdateCancel",
    value: function handlePageUpdateCancel() {
      this.setState({
        editRecord: null,
        updateModalProps: {
          visible: false
        }
      });
    }
  }, {
    key: "handlePageDesign",
    value: function handlePageDesign(pageCode) {
      var dispatch = this.props.dispatch;
      dispatch(routerRedux.push({
        pathname: "/cpfm/ui/page-org/detail/".concat(pageCode)
      }));
    }
  }]);

  return PageList;
}(React.Component), (_applyDecoratedDescriptor(_class2.prototype, "handleCreateBtnClick", [_dec3], Object.getOwnPropertyDescriptor(_class2.prototype, "handleCreateBtnClick"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handlePageCreate", [_dec4], Object.getOwnPropertyDescriptor(_class2.prototype, "handlePageCreate"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handlePageCreateCancel", [_dec5], Object.getOwnPropertyDescriptor(_class2.prototype, "handlePageCreateCancel"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleRefSearchForm", [_dec6], Object.getOwnPropertyDescriptor(_class2.prototype, "handleRefSearchForm"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleSearch", [_dec7], Object.getOwnPropertyDescriptor(_class2.prototype, "handleSearch"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleTableChange", [_dec8], Object.getOwnPropertyDescriptor(_class2.prototype, "handleTableChange"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handlePageEdit", [_dec9], Object.getOwnPropertyDescriptor(_class2.prototype, "handlePageEdit"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handlePageUpdate", [_dec10], Object.getOwnPropertyDescriptor(_class2.prototype, "handlePageUpdate"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handlePageUpdateCancel", [_dec11], Object.getOwnPropertyDescriptor(_class2.prototype, "handlePageUpdateCancel"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handlePageDesign", [_dec12], Object.getOwnPropertyDescriptor(_class2.prototype, "handlePageDesign"), _class2.prototype)), _class2)) || _class) || _class);
export { PageList as default };